'use strict';
let Response = require('../../model/Response'),
    RestifyProxy = require('../../util/RestifyProxy');

let _appendChildren = function (nodes) {
    let html = [];
    html.push(`<ul module-menu="">`);
    for (let i in nodes) {
        html.push(`<li module-menu="${nodes[i].url}"
                module-target="page">${nodes[i].name}`);
        if (nodes[i].children && nodes[i].children.length > 0) {
            html.push(_appendChildren(nodes[i].children));
        }
        html.push('</li>');
    }
    html.push('</ul>');
    return html.join('');
};
let _appendNodes = function (menuArray) {
    let html = [];
    for (let i in menuArray) {
        html.push(`<div class="sub"
           ><h4  module-menu="${menuArray[i].url}"
             module-target="page">
            <div class="menu-icon ${menuArray[i].icon}"></div>
            <span class="text">${menuArray[i].name}</span>`);
        if (menuArray[i].children && menuArray[i].children.length > 0) {
            html.push(`<i class="menu-right"></i></h4>`);
            html.push(_appendChildren(menuArray[i].children));
        }else{
            html.push(`</h4>`);
        }
        html.push(`</div>`);
    }
    return html.join('');
};


/**
 * 后台主页面
 */
module.exports.backPage = function () {
    let req = this.req;
    let user = req.user;
    user.menus=[
        {
            "id": 1,
            "parentId": -1,
            "code": "home",
            "name": "首页",
            "icon": "homepage",
            "style": "homepage",
            "url": "/back/homepage/page",
            "level": 1
        },
        /*{
            "id": 131,
            "parentId": -1,
            "code": "Bootstrap",
            "name": "数据表格",
            "icon": "",
            "style": "Bootstrap",
            "url": "",
            "level": 1
        },
        {
            "id": 132,
            "parentId": 131,
            "code": "account",
            "name": "Bootstrap-table",
            "icon": "",
            "style": "",
            "url": "/back/Bootstrap/Bootstrap-table/page",
            "level": 2
        },*/
        {
            "id": 2,
            "parentId": -1,
            "code": "",
            "name": "告警管理",
            "icon": "alarm",
            "style": "",
            "url": "/back/alarm/page",
            "level": 1
        },
        {
            "id": 3,
            "parentId": -1,
            "code": "workorder",
            "name": "工单管理",
            "icon": "order",
            "style": "",
            "url": "",
            "level": 1
        },
        {
            "id": 31,
            "parentId": 3,
            "code": "order",
            "name": "工单管理",
            "icon": "",
            "style": "",
            "url": "/back/workorder/order/page",
            "level": 2
        },
        {
            "id": 32,
            "parentId": 3,
            "code": "backlog",
            "name": "待办工单",
            "icon": "",
            "style": "",
            "url": "/back/workorder/backlog/page",
            "level": 2
        },
        {
            "id":4,
            "parentId": -1,
            "code": "operate",
            "name": "运营管理",
            "icon": "operate",
            "style": "",
            "url": "",
            "level": 1
        },
        {
            "id":41,
            "parentId": 4,
            "code": "singleLamp",
            "name": "单灯管理",
            "icon": "",
            "style": "",
            "url": "/back/operate/singleLamp/page",
            "level": 2
        },
        {
            "id":42,
            "parentId": 4,
            "code": "groups",
            "name": "分组管理",
            "icon": "",
            "style": "",
            "url": "/back/operate/groups/page",
            "level": 2
        },
        {
            "id":43,
            "parentId": 4,
            "code": "project",
            "name": "项目管理",
            "icon": "",
            "style": "",
            "url": "/back/operate/project/page",
            "level": 2
        },
        {
            "id":44,
            "parentId": 4,
            "code": "lightmethod",
            "name": "照明策略",
            "icon": "",
            "style": "",
            "url": "/back/operate/lightmethod/page",
            "level": 2
        },
        {
            "id":45,
            "parentId": 4,
            "code": "sim",
            "name": "SIM卡管理",
            "icon": "",
            "style": "",
            "url": "/back/operate/sim/page",
            "level": 2
        },
        {
            "id":46,
            "parentId": 4,
            "code": "sms",
            "name": "短信设置",
            "icon": "",
            "style": "",
            "url": "/back/operate/sms/page",
            "level": 2
        },
        {
            "id":5,
            "parentId": -1,
            "code": "run",
            "name": "运行监控",
            "icon": "run",
            "style": "",
            "url": "/back/runMontior/page",
            "level": 1
        },
        {
            "id":6,
            "parentId": -1,
            "code": "report",
            "name": "数据报表",
            "icon": "report",
            "style": "",
            "url": "",
            "level": 1
        },
        {
            "id": 61,
            "parentId":6,
            "code": "alarm-info",
            "name": "告警信息",
            "icon": "",
            "style": "",
            "url": "/back/report/alarmInfo/page",
            "level": 2
        },
        {
            "id": 62,
            "parentId":6,
            "code": "log-message",
            "name": "报文日志",
            "icon": "",
            "style": "",
            "url": "/back/report/logMessage/page",
            "level": 2
        },
        {
            "id": 63,
            "parentId":6,
            "code": "singleLight-condition",
            "name": "单灯工况",
            "icon": "",
            "style": "",
            "url": "/back/report/singleLightCondition/page",
            "level": 2
        },
        {
            "id": 64,
            "parentId":6,
            "code": "work-order",
            "name": "工单信息",
            "icon": "",
            "style": "",
            "url": "/back/report/workOrder/page",
            "level": 2
        },
        {
            "id": 65,
            "parentId":6,
            "code": "history-time",
            "name": "历史开关灯时间",
            "icon": "",
            "style": "",
            "url": "/back/report/historyTime/page",
            "level": 2
        },
       /* {
            "id": 66,
            "parentId":6,
            "code": "terminal-report",
            "name": "终端报表",
            "icon": "",
            "style": "",
            "url": "/back/report/terminalReport/page",
            "level": 2
        },*/
        {
            "id": 7,
            "parentId": -1,
            "code": "system",
            "name": "系统设置",
            "icon": "system",
            "style": "",
            "url": "",
            "level": 1
        },
        {
            "id": 72,
            "parentId": 7,
            "code": "account-management",
            "name": "账户管理",
            "icon": "",
            "style": "",
            "url": "/back/system/accountManagement/page",
            "level": 2
        },
        {
            "id": 73,
            "parentId": 7,
            "code": "account-management",
            "name": "角色管理",
            "icon": "",
            "style": "",
            "url": "/back/system/roleManagement/page",
            "level": 2
        },
        {
            "id": 71,
            "parentId": 7,
            "code": "function-permission",
            "name": "功能权限管理",
            "icon": "",
            "style": "",
            "url": "/back/system/permission/functionPage",
            "level": 2
        },
        {
            "id": 72,
            "parentId": 7,
            "code": "data-permission",
            "name": "数据权限管理",
            "icon": "",
            "style": "",
            "url": "/back/system/permission/dataPage",
            "level": 2
        },
        {
            "id": 75,
            "parentId": 7,
            "code": "organize",
            "name": "组织机构管理",
            "icon": "",
            "style": "",
            "url": "/back/system/organize/page",
            "level": 2
        },
        {
            "id": 76,
            "parentId": 7,
            "code": "systempara",
            "name": "系统参数管理",
            "icon": "",
            "style": "",
            "url": "/back/system/systempara/page",
            "level": 2
        },
        {
            "id": 77,
            "parentId": 7,
            "code": "dictionary",
            "name": "数据字典管理",
            "icon": "",
            "style": "",
            "url": "/back/system/dictionary/page",
            "level": 2
        },
        {
            "id": 8,
            "parentId": -1,
            "code": "logs",
            "name": "日志中心",
            "icon": "logs",
            "style": "",
            "url": "",
            "level": 1
        },
        {
            "id": 81,
            "parentId": 8,
            "code": "run",
            "name": "运行日志",
            "icon": "",
            "style": "",
            "url": "/back/logs/runPage",
            "level": 2
        },
        {
            "id": 82,
            "parentId": 8,
            "code": "operate",
            "name": "操作日志",
            "icon": "",
            "style": "",
            "url": "/back/logs/operatePage",
            "level": 2
        }
    ];
    let menusNodes = JSON.parse(JSON.stringify(user.menus));
    let menuArray = [];
    let menuMap = {};
    for (let i = 0, l = menusNodes.length; i < l; i++) {
        menuMap[menusNodes[i].id] = menusNodes[i];
    }
    for (let i = 0, l = menusNodes.length; i < l; i++) {
        if (menuMap[menusNodes[i].parentId] && menusNodes[i].id != menusNodes[i].parentId) {
            if (!menuMap[menusNodes[i].parentId].children)
                menuMap[menusNodes[i].parentId].children = [];
            menuMap[menusNodes[i].parentId].children.push(menusNodes[i]);
        } else {
            menuArray.push(menusNodes[i]);
        }
    }
    let menuHtml = _appendNodes(menuArray);
    let reqData = {user, menuHtml};
    let response = new Response();
    response.render('back/back', reqData);
    this.resolve(response);
};